package org.academic.texteditor.gui.form;

import java.awt.LayoutManager;

import javax.swing.JInternalFrame;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import org.academic.texteditor.gui.PrincipalWindow;
import org.academic.texteditor.gui.component.JGlassPanel;
import org.academic.texteditor.helper.MessageHelper;

public abstract class AbstractModalFrame extends AbstractInternalFrame {

	private static final long serialVersionUID = 2636143014227342509L;

	protected static MessageHelper msg = MessageHelper.getInstance();

	protected AbstractModalFrame(String title) {
		this(title, null);
	}

	protected AbstractModalFrame(String title, LayoutManager layout) {
		super(title, layout);
		
		setClosable(true);
		setResizable(false);
		setIconifiable(false);
		setMaximizable(false);
		setVisible(true);
		
		AncestorListener listener = new AncestorListener() {
			@Override
			public void ancestorAdded(AncestorEvent event) {
				
			}
			@Override
			public void ancestorMoved(AncestorEvent event) {
				
			}
			@Override
			public void ancestorRemoved(AncestorEvent event) {
				JGlassPanel glassPane = PrincipalWindow.getInstance().getGlassPane();
				JInternalFrame[] allFrames = glassPane.getAllFrames();
				if (allFrames.length <= 1) {
					glassPane.setVisible(false);
				}
			}};
		addAncestorListener(listener);
	}

	protected abstract void createInterface();

}
